package org.seven.jrdp.commons.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.seven.jrdp.commons.constant.GlobalContants;

public abstract class DateTimeUtils {
	public static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	public static Date getDate() {
		return getCalendar().getTime();
	}

	public static long getTime() {
		return getDate().getTime();
	}

	public static Date getMonthFirst() {
		Calendar calendar = getCalendar();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	public static Date getMonthLast() {
		Calendar calendar = getCalendar();
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		return calendar.getTime();
	}

	public static boolean isTime(Date date) {
		Calendar calendar = DateUtils.toCalendar(date);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		int millisecond = calendar.get(Calendar.MILLISECOND);
		return hour + minute + second + millisecond > 0;
	}

	public static boolean isTime(Timestamp timestamp) {
		return isTime(new Date(timestamp.getTime()));
	}

	public static String getWeek(Date date) {
		String[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		int i = DateUtils.toCalendar(date).get(Calendar.DAY_OF_WEEK) - 1;
		return week[i];
	}

	public static Date getDate(String s) {
		try {
			return new SimpleDateFormat(GlobalContants.PATTERN_DATE).parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static Date getDate(String s, String pattern) {
		try {
			return new SimpleDateFormat(pattern).parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}